package com.nciae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nciae.po.Admin;
import com.nciae.dao.AdminMapper;
import com.nciae.po.User;
import com.nciae.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Guoliyu
 * @since 2022-03-30
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public List<Admin> findAll(int index, int capacity, String username) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!Objects.isNull(username)) {
            wrapper.like("username", username);
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public void add(Admin admin) {
        baseMapper.insert(admin);
    }

    @Override
    public void update(Admin admin) {
        baseMapper.updateById(admin);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public Admin findAdminById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Admin login(String username, String password) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
        Admin admin = baseMapper.selectOne(wrapper);
        if (Objects.isNull(admin)) {
            throw new RuntimeException();
        }
        return admin;
    }
}
